package com.gxa.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.gxa.common.base.RespResult;
import com.gxa.domain.dto.PlanListDto;
import com.gxa.domain.entity.CablePlan;
import com.gxa.domain.entity.PlanList;
import com.gxa.service.PlanListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 吴邓江
 * 大帅哥
 */
@Controller
@Slf4j
@RequestMapping("/logistics_PlanList")
@Api(tags = "计划列表")
public class PlanListController {
    @Autowired
    private PlanListService planListService;

    /**
     * 获取电缆方案列表
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取电缆方案列表", notes = "获取电缆方案列表")
    @ResponseBody
    public RespResult list() {
        log.info("获取电缆方案列表");
        List<PlanList> list = planListService.list();
        return RespResult.success(list);
    }

    /**
     * 新增计划方案
     *
     * @param planList
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增电缆方案", notes = "新增电缆方案")
    @ResponseBody
    public RespResult save(@RequestBody PlanList planList) {
        log.info("新增电缆方案信息: {}", planList);
        planListService.save(planList);
        return RespResult.success();
    }

    /**
     * 根据id的到计划方案详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取电缆方案详情", notes = "获取电缆方案详情")
    @ResponseBody
    public RespResult get(@PathVariable("id") Integer id) {
        log.info("获取电缆方案详情: {}", id);
        PlanList planList = planListService.getById(id);
        return RespResult.success(planList);
    }

    /**
     * 修改计划方案
     *
     * @param planList
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新电缆方案", notes = "更新电缆方案")
    @ResponseBody
    public RespResult update(@RequestBody PlanList planList) {
        log.info("更新电缆方案信息: {}", planList);
        planListService.updateById(planList);
        return RespResult.success();
    }

    @PostMapping("/{id}")
    @ApiOperation(value = "删除电缆方案", notes = "删除电缆方案")
    @ResponseBody
    public RespResult delete(@PathVariable("id") Integer id) {
        log.info("删除电缆方案: {}", id);
        planListService.removeById(id);
        return RespResult.success();
    }


    /**
     * 分页查询电缆方案列表
     *
     * @param planListDto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询电缆方案列表", notes = "分页查询电缆方案列表")
    @ResponseBody
    public RespResult page(PlanListDto planListDto) {
        log.info("分页查询电缆方案列表", planListDto);
        return planListService.pagequery(planListDto);
    }

    /**
     * excel表格上传
     * @param file excel表格文件
     */
    @PostMapping("/import")
    @ApiOperation(value = "计划列表excel表格上传", notes = "计划列表excel表格上传")
    public RespResult imp(@RequestParam("file") MultipartFile file) throws Exception {
        String fileName = "E:\\op\\项目② 物流管理系统\\计划导入模板excel\\"+file.getOriginalFilename();
        log.info("上传文件名: {}", fileName);
        EasyExcel.read(fileName, PlanList.class, new PageReadListener<PlanList>(planLists ->{
            planListService.saveBatch(planLists);
        } )).sheet().doRead();
        return RespResult.success();
    }


}
